package com.hunliji.monitor.web.facade.impl;

import com.hunliji.common.exception.WarnException;
import com.hunliji.monitor.service.api.DbInstanceService;
import com.hunliji.monitor.service.api.model.bo.DbInstanceBO;
import com.hunliji.monitor.share.component.db.DbHandlerFacade;
import com.hunliji.monitor.web.facade.DbFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * 数据库操作
 * @author wuchen
 */
@Service
public class DbFacadeImpl implements DbFacade {
    private static final Logger logger = LoggerFactory.getLogger(DbFacadeImpl.class);
    @Resource
    private DbInstanceService dbInstanceService;
    @Resource
    private DbHandlerFacade dbHandlerFacade;
    @Override
    public void initDbFromInstance(Long instanceId) {
        //进行数据库实例查询
        DbInstanceBO dbInstanceBO = dbInstanceService.queryById(instanceId);
        if(Objects.isNull(dbInstanceBO)){
            throw new WarnException("无法查询到对应的数据库实例");
        }
        //获取对应的表
        String sql = "select schema_name from information_schema.schemata;";
        dbHandlerFacade.executeQuerySql(sql,dbInstanceBO.getNetworkAddress(),"ad_business",dbInstanceBO.getAdminAccount(),dbInstanceBO.getPassword());
    }
}
